﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace SqliteManager.SQLite3
{
    public interface IDBWrite
    {
        
    }

    /// <summary>
    /// 所有可以写入数据的基类
    /// </summary>
    public class DataBaseWriter
    {
        /// <summary>
        /// 存放对于特殊名称的标记时调用的回调
        /// <para>例如 时间，GUID等</para>
        /// <para>默认 Date 和 GUID 会自动加入</para>
        /// </summary>
        public static Dictionary<string, Func<string>> WriterFuncSet = new Dictionary<string, Func<string>>()
        {
            { "DATE", ()=>{ return DateTime.Now.ToString(); } },
            { "GUID", ()=>{ return Guid.NewGuid().ToString(); } },
        };

        /// <summary>
        /// 获取插入数据库的SQL命令
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="listDBName"></param>
        /// <returns></returns>
        public static string GetInsertSQL(object obj,Stack<string> listDBName)
        {

            Type t = obj.GetType();

            string result = string.Empty;
            StringBuilder sbcol = new StringBuilder();
            StringBuilder sbval = new StringBuilder();

            while (listDBName.Count>0)
            {
                string key = listDBName.Pop();
                string value = string.Empty;
                bool isend = !(listDBName.Count > 0);

                if (WriterFuncSet.ContainsKey(key))
                {
                    value = WriterFuncSet[key].Invoke();
                    goto BuilderEnd;
                }      
                FieldInfo[] fies = t.GetFields();
                PropertyInfo[] proes = t.GetProperties();

                if (fies.Length > 0)
                {
                    FieldInfo fi = null;
                    try
                    {
                        fi = fies.First(f => f.Name == key);
                    }
                    catch (Exception e)
                    {
                        
                    }
                    if (fi != null)
                    {
                        value = fi.GetValue(obj).ToString();
                        goto BuilderEnd;
                    }

                }

                if(proes.Length>0)
                {
                    PropertyInfo pi = null;
                    try
                    {
                        pi = proes.First(p => p.Name == key);
                    }
                    catch (Exception e)
                    {

                    }
                    if (pi != null)
                    {
                        value = pi.GetValue(obj, null).ToString();
                        goto BuilderEnd;
                    }
                }
                BuilderEnd:
                if(!string.IsNullOrEmpty(value))
                {
                    sbcol.Append(key);
                    sbval.Append("'"+value+"'");
                    if (!isend)
                    {
                        sbcol.Append(",");
                        sbval.Append(",");
                    }
                }

            }

            if (sbcol.Length<=0)
            {
                return string.Empty;
            }

            return string.Format(" ({0}) VALUES ({1})", sbcol.ToString(), sbval.ToString());
        }
    }
}
